UUID.js - RFC-compliant UUID Generator for JavaScript
Synopsis
<script src="src/uuid.js"></script>
<script> var uuid = UUID.generate(); </script>
let UUID = require("uuidjs");
let uuid = UUID.generate();
import UUID from "uuidjs";
let str: string = UUID.generate();
let obj: UUID = UUID.genV4();
npx uuidjs
Description
UUID.js is a JavaScript/ECMAScript library to generate RFC 4122/9562 compliant
Universally Unique IDentifiers (UUIDs). This library supports both version 4
UUIDs (UUIDs from random numbers) and version 1 UUIDs (time-based UUIDs), and
provides an object-oriented interface to print a generated or parsed UUID in a
variety of forms.
Features
- Generates version 4 UUIDs (UUIDs from random numbers) and version 1 UUIDs
(time-based UUIDs)
- Provides an object-oriented interface to print various string representations
of a generated or parsed UUID
- Utilizes a cryptographically secure pseudo-random number generator if
available, whereas falling back to
Math.random()
otherwise - Appends extra random bits to compensate for the lower timestamp resolution of
JavaScript than that required for version 1 UUIDs
- Comes with a lot of test cases including format checks and statistical tests
to maintain a high-quality code base
- Supports old browsers as well as modern browser and server environments, as
kept compatible with ECMAScript 3rd edition
Install
Download src/uuid.js
or call npm install uuidjs
.
Then, load src/uuid.js
.
<script src="src/uuid.js"></script>
Or, import uuidjs
.
const UUID = require("uuidjs");
Usage Examples
UUID.generate()
returns a version 4 UUID as a hexadecimal string.
console.log(UUID.generate());
UUID.genV4()
, UUID.genV1()
, and UUID.parse()
return a UUID object that has
various fields and methods.
var objV4 = UUID.genV4();
var objV1 = UUID.genV1();
var uuid = UUID.parse("a0e0f130-8c21-11df-92d9-95795a3bcd40");
console.log(uuid.toString());
console.log(uuid.hexString);
console.log(uuid.hexNoDelim);
console.log(uuid.bitString);
console.log(uuid.urn);
console.log(objV4.equals(objV1));
console.log(objV4.version);
console.log(objV1.version);
console.log(uuid.intFields.timeLow);
console.log(uuid.bitFields.timeMid);
console.log(uuid.hexFields.timeHiAndVersion);
console.log(uuid.intFields.clockSeqHiAndReserved);
console.log(uuid.bitFields.clockSeqLow);
console.log(uuid.hexFields.node);
console.log(uuid.intFields[0]);
console.log(uuid.bitFields[1]);
console.log(uuid.hexFields[2]);
console.log(uuid.intFields[3]);
console.log(uuid.bitFields[4]);
console.log(uuid.hexFields[5]);
UUID.js supports the so-called noConflict mode to work around namespace
conflicts.
var arbitraryVarName = UUID;
UUID = UUID.overwrittenUUID;
console.log(arbitraryVarName.generate());
License
Copyright (c) 2010-2024 LiosK
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
Author
LiosK contact@mail.liosk.net
See Also